package cn.wolfcode.shop.cloud.web.controller;

import cn.wolfcode.shop.cloud.RedisService;
import cn.wolfcode.shop.cloud.Result;
import cn.wolfcode.shop.cloud.service.ISecKillGoodsService;
import cn.wolfcode.shop.cloud.util.SecKillKeyPrefix;
import cn.wolfcode.shop.cloud.vo.SecKillGoodVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/secKillGood")
public class SecKillGoodController {

    @Autowired
    private ISecKillGoodsService secKillGoodsService;

    @Autowired
    private RedisService redisService;

    @RequestMapping("/find")
    public Result<SecKillGoodVO> find(Long seckillId){
        return Result.success(secKillGoodsService.find(seckillId));
    }


    @RequestMapping("/query")
    public Result query() {
        return Result.success(secKillGoodsService.queryByCache());
    }

    @RequestMapping("/initData")
    public Result<String> initData(){
        List<SecKillGoodVO> list = secKillGoodsService.query();
        for (SecKillGoodVO seckillGoodVO : list) {
            redisService.set(SecKillKeyPrefix.SECKILL_STOCK,seckillGoodVO.getId()+"",seckillGoodVO.getStockCount());

            //把秒杀商品也放入到redis当中
            redisService.hset(SecKillKeyPrefix.SECKILL_GOOD,"",seckillGoodVO.getId()+"",seckillGoodVO);

        }
        return Result.success("初始化成功");
    }


}
